package com.suoluo.zenadmin.controllers.portal;

import java.util.List;

import javax.annotation.Resource;

import net.paoding.rose.web.Invocation;
import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;

import com.suoluo.zenadmin.service.bigimage.BigImageService;
import com.suoluo.zenadmin.service.friend.FriendService;
import com.suoluo.zenadmin.service.home.HomeService;
import com.suoluo.zenadmin.vo.bigimage.BigImageVO;
import com.suoluo.zenadmin.vo.friend.FriendItemVO;
import com.suoluo.zenadmin.vo.friend.FriendTypeVO;
import com.suoluo.zenadmin.vo.home.HomeCategoryVO;

@Path("")
public class HomeController {

	@Resource(name="bigImageService")
	private BigImageService bigImageService;
	
	@Resource(name="friendService")
	private FriendService friendService;
	
	@Resource(name="homeService")
	private HomeService homeService;
	@Get("/")
	public String home(Invocation inv){
		
		List<HomeCategoryVO> categoryList = homeService.findAllHomeData();
		inv.addModel("categoryList",categoryList);
		inv.addModel("bigImages", bigImageService.findBigImageByType(BigImageVO.IMAGE_TYPE_HOME));
		return "home";
	}
	
	
	/**
	 * 关于我们
	 */
	@Get("about")
	public String showAbout(){
		return "about";
	}
	
	/**
	 * 隐私声明
	 */
	@Get("privacy")
	public String showPrivacy(){
		return "privacyStatement";
	}
	
	/**
	 * 网站声明
	 */
	@Get("website")
	public String showWebiste(){
		return "networkStatement";
	}
	
	/**
	 * 跳转到友情链接类别管理页面
	 * @param request
	 * @param response
	 * @return
	 */
	@Get("friend")
	public String showFriends(Invocation inv,@Param("id") int id)
	{
		//获取所有的友情链接类别信息
		List<FriendTypeVO> listType = friendService.findListFriendType();
		inv.addModel("listType",listType);
		//要展示的友情链接的类别Id信息
	
			if(id == 0 && !listType.isEmpty())
			{
				id = listType.get(0).getFriendTypeId();
			}
			inv.addModel("friendTypeId",id);
		//获取所有的友情链接类别项信息
		List<FriendItemVO> listItem = friendService.findListFriendItem(id);
		inv.addModel("listItem",listItem);
		
		return "friends";
	}
	
	@Get("404")
	public String show404(Invocation inv){
		Invocation preInv = inv.getPreInvocation();
		int recursiveCount = 0;
		while (preInv != null){
			recursiveCount++;
			preInv = preInv.getPreInvocation();
		}
		//如果重复出现递归404错误，则直接返回
		if (recursiveCount >= 2){
			inv.getRequest().removeAttribute("javax.servlet.error.exception_type");
			inv.getRequest().removeAttribute("javax.servlet.error.message");
			inv.getRequest().removeAttribute("javax.servlet.error.exception");
			return "@server error";
		}
		List<HomeCategoryVO> categoryList = homeService.findAllHomeData();
		inv.addModel("categoryList",categoryList);
		return "404";
	}
}
